package com.health.activity.tijian;

import java.util.ArrayList;

import com.health.NovaFragmentActivity;
import com.health.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.tijian.HealthTiJian_CTXXFragment;
import com.health.fragment.tijian.HealthTiJian_FzjcFragment;
import com.health.fragment.tijian.HealthTiJian_HealthquesFragment;
import com.health.fragment.tijian.HealthTiJian_JkpjFragment2;
import com.health.fragment.tijian.HealthTiJian_ShfsFragment;
import com.health.fragment.tijian.HealthTiJian_YbqkFragment;
import com.health.fragment.tijian.HealthTiJian_ZLQKFragment;
import com.health.fragment.tijian.HealthTiJian_ZytzFragment;
import com.health.tools.GlobalConts;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * 健康体检
 * 
 * @author nova
 *
 */
public class TiJianActivity extends NovaFragmentActivity implements OnClickListener {
	protected static final String TAG = "TiJianActivity";
	private ViewPager viewPager;
	private ArrayList<RadioButton> radioButtons;
	private ArrayList<Fragment> fragments;

	private TextView title;
	private Button backBtn;
	private Button saveTijianBtn;
	private FragmentAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);	
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_tijian);
		initView();
		addListener();
	}

	private void initView() {

		viewPager = (ViewPager) findViewById(R.id.viewpager);
		RadioButton tijian_ybqk = (RadioButton) findViewById(R.id.tijian_ybqk);
		RadioButton tijian_shfs = (RadioButton) findViewById(R.id.tijian_shfs);
		RadioButton tijian_ctxx = (RadioButton) findViewById(R.id.tijian_ctxx);
		RadioButton tijian_fzjc = (RadioButton) findViewById(R.id.tijian_fzjc);
		RadioButton tijian_zytz = (RadioButton) findViewById(R.id.tijian_zytz);
		RadioButton tijian_jkwt = (RadioButton) findViewById(R.id.tijian_jkwt);
		RadioButton tijian_zlqk = (RadioButton) findViewById(R.id.tijian_zlqk);
		RadioButton tijian_jkpj = (RadioButton) findViewById(R.id.tijian_jkpj);

		radioButtons = new ArrayList<RadioButton>();
		radioButtons.add(tijian_ybqk);
		radioButtons.add(tijian_shfs);
		radioButtons.add(tijian_ctxx);
		radioButtons.add(tijian_fzjc);
		radioButtons.add(tijian_zytz);
		radioButtons.add(tijian_jkwt);
		radioButtons.add(tijian_zlqk);
		radioButtons.add(tijian_jkpj);

		tijian_jkpj.setOnClickListener(this);
		tijian_ybqk.setOnClickListener(this);
		tijian_shfs.setOnClickListener(this);
		tijian_ctxx.setOnClickListener(this);
		tijian_fzjc.setOnClickListener(this);
		tijian_zytz.setOnClickListener(this);
		tijian_jkwt.setOnClickListener(this);
		tijian_zlqk.setOnClickListener(this);

		Fragment baseFragement = new HealthTiJian_YbqkFragment();
		Fragment cqsf1Fragment = new HealthTiJian_ShfsFragment();
		Fragment cqsf2Fragment = new HealthTiJian_CTXXFragment();
		Fragment cqsf3Fragment = new HealthTiJian_FzjcFragment();
		Fragment cqsf4Fragment = new HealthTiJian_ZytzFragment();
		Fragment health_quesFragment = new HealthTiJian_HealthquesFragment();
		Fragment zlqkFragment = new HealthTiJian_ZLQKFragment();
		Fragment ch42Fragment = new HealthTiJian_JkpjFragment2();
		fragments = new ArrayList<Fragment>();
		fragments.add(baseFragement);
		fragments.add(cqsf1Fragment);
		fragments.add(cqsf2Fragment);
		fragments.add(cqsf3Fragment);
		fragments.add(cqsf4Fragment);
		fragments.add(health_quesFragment);
		fragments.add(zlqkFragment);
		fragments.add(ch42Fragment);

		adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
		viewPager.setAdapter(adapter);
		View head = findViewById(R.id.tijian_title);
		title = (TextView) head.findViewById(R.id.title);
		title.setText("健康体检--一般情况");
		saveTijianBtn = (Button) head.findViewById(R.id.btn_up);
		backBtn = (Button) head.findViewById(R.id.btn_back);
		saveTijianBtn.setOnClickListener(this);
		backBtn.setOnClickListener(this);
		setChecked(0);
	}

	private void addListener() {

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				radioButtons.get(position).setChecked(true);
				setChecked(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});

	}

	// 设置监听
	public void setTitle(String titleString) {
		title.setText(titleString);
	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.tijian_ybqk) {
			viewPager.setCurrentItem(0);
			setChecked(0);
		} else if (id == R.id.tijian_shfs) {
			viewPager.setCurrentItem(1);
			setChecked(1);
		} else if (id == R.id.tijian_ctxx) {
			viewPager.setCurrentItem(2);
			setChecked(2);
		} else if (id == R.id.tijian_fzjc) {
			viewPager.setCurrentItem(3);
			setChecked(3);
		} else if (id == R.id.tijian_zytz) {
			viewPager.setCurrentItem(4);
			setChecked(4);
		} else if (id == R.id.tijian_jkwt) {
			viewPager.setCurrentItem(5);
			setChecked(5);
		} else if (id == R.id.tijian_zlqk) {
			viewPager.setCurrentItem(6);
			setChecked(6);
		} else if (id == R.id.tijian_jkpj) {
			viewPager.setCurrentItem(7);
			setChecked(7);
		} else if (id == R.id.btn_back) {
			this.finish();
		} else if (id == R.id.btn_up) {
			// 保存
			onFragmentSave currentFragment = (onFragmentSave) adapter.getCurrentFragment();
			currentFragment.save();
		} else {
		}

	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		Log.e(TAG, requestCode+"\n--"+ resultCode);
		onFragmentSave fragment = (onFragmentSave) adapter.getCurrentFragment();
		if (requestCode == GlobalConts.RequestXueya && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultXueya) {
			Log.e(TAG, data.getStringExtra("高压")+"\n"+ data.getStringExtra("低压")+"\n"+ data.getStringExtra("脉率"));
			fragment.setXueya(data.getStringExtra("高压"), data.getStringExtra("低压"), data.getStringExtra("脉率"));
		} else if (requestCode == GlobalConts.RequestTizhong && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultTizhong) {
			fragment.setTizhong(data.getStringExtra("value"));
		} else if (requestCode == GlobalConts.RequestTiwen && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultTiwen) {
			Log.w("eeeeeeeee", data.getStringExtra("value"));
			fragment.setTiWen(data.getStringExtra("value"));
		}  else if (requestCode == GlobalConts.RequestMaiLv && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultMaiLv) {
			Log.w("eeeeeeeee", data.getStringExtra("value"));
			fragment.setMaiLv(data.getStringExtra("value"));
		}  else if (requestCode == GlobalConts.RequestXueHongDanbai && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultXueHongDanbai) {
			Log.w("eeeeeeeee", data.getStringExtra("value"));
			fragment.setXueHongDanBai(data.getStringExtra("value"));
		}  else if (requestCode == GlobalConts.RequestXuetang && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultXuetang) {
			Log.w("eeeeeeeee", data.getStringExtra("value"));
			fragment.setXuetang(data.getStringExtra("value"));
		} else if(requestCode == GlobalConts.RequestECG && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultECG){
			fragment.setLiBangECG(data.getStringExtra("result"), data.getStringExtra("time"));
		} else if(requestCode==GlobalConts.RequestNiaoChangGui&&resultCode==com.novabluetooth.views.ExamHistoryActivity.ResultNiaoChangGui){
			fragment.setNiaoYeFenxi(data.getStringExtra("尿胆原"), data.getStringExtra("尿潜血"), data.getStringExtra("胆红素"), data.getStringExtra("尿酮体"), data.getStringExtra("尿糖"), data.getStringExtra("蛋白质"), data.getStringExtra("PH"), data.getStringExtra("亚硝酸盐"), data.getStringExtra("白细胞"), data.getStringExtra("比重"), data.getStringExtra("维生素"));
		}
	}
	@SuppressLint("ResourceAsColor")
	private void setChecked(int pos) {
		for (RadioButton rbtn : radioButtons) {
			rbtn.setBackground(null);
		}
		radioButtons.get(pos).setBackgroundResource(R.drawable.rd_chek_center);
		if(pos==0){
			title.setText("健康体检  —— 一般情况");
		}else if(pos==1){
			title.setText("健康体检  —— 生活方式");
		}else if(pos==2){
			title.setText("健康体检  —— 查体信息");
		}else if(pos==3){
			title.setText("健康体检  —— 辅助检查");
		}else if(pos==4){
			title.setText("健康体检  —— 中医体质");
		}else if(pos==5){
			title.setText("健康体检  —— 健康问题");
		}else if(pos==6){
			title.setText("健康体检  —— 治疗情况");
		}else if(pos==7){
			title.setText("健康体检  —— 健康评价");
		}
	
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.cancelTextDialog();
	}
}
